package org.example.game.history;

import java.util.Arrays;

/**
 * @Description: TODO
 * @Author wyatt
 * @Data 2024/06/09 10:59
 */
public class Solution100319 {

    public static void main(String[] args) {
        Solution100319 solution100319 = new Solution100319();
        System.out.println(solution100319.maxTotalReward(new int[]{1,1,3,3}));
    }

    public int maxTotalReward(int[] rewardValues) {

        //从小到大排序
        //重复的不能多次抢，先剔除
        Arrays.sort(rewardValues);
        int min = rewardValues[0];
        int max = rewardValues[rewardValues.length-1];

        int repCount = 0;
        for (int i = 1; i < rewardValues.length; i++) {
            if(rewardValues[i] == rewardValues[i-1]){
                repCount++;
                rewardValues[i] = 0;
            }
        }

        int[] nums = new int[rewardValues.length - repCount];
        Arrays.sort(rewardValues);
        for(int i = repCount;i<rewardValues.length;i++){
            nums[i-repCount] = rewardValues[i];

        }
        return max + getNextCount(nums, max, min, nums.length-1);
    }

    private int getNextCount(int[] nums, int max, int min, int index) {
        int nextMaxSum = 0;
        for(int i=index-1;i>=0;i--){
            int subSum = 0;
            if(nums[i] < max){
                if( max - nums[i] < min){
                    subSum = nums[i];
                }else {
                    int nextMax = Math.min(max - nums[i], nums[i]);
                    if(nextMax <= nums[i]){
                        subSum = nums[i] + getNextCount(nums, nextMax , min, i);
                    }
                }
            }
            nextMaxSum = Math.max(nextMaxSum, subSum);
        }
        return nextMaxSum;
    }



    public int maxTotalReward2(int[] rewardValues) {

        //从小到大排序
        //重复的不能多次抢，先剔除
        Arrays.sort(rewardValues);
        int min = rewardValues[0];
        int max = rewardValues[rewardValues.length-1];

        int repCount = 0;
        for (int i = 1; i < rewardValues.length; i++) {
            if(rewardValues[i] == rewardValues[i-1]){
                repCount++;
                rewardValues[i] = 0;
            }
        }


        int[] nums = new int[rewardValues.length - repCount];
        Arrays.sort(rewardValues);
        for(int i = repCount;i<rewardValues.length;i++){
            nums[i-repCount] = rewardValues[i];

        }

        int sum = max;
//        while(true){
//            getNext(nums,max/2);
//            getLeft();
//        }


        return 0;
    }

}
